package com.snail.readyvr.mvp.presenter;

import android.content.Context;

import com.snail.readyvr.mvp.contract.SettingsActivityContract;
import com.snail.readyvr.mvp.model.LoginResponse;
import com.snail.readyvr.rest.HttpResultFunc;
import com.snail.readyvr.rest.LoginRestClient;
import com.snail.readyvr.rest.RestApiUtils;
import com.snail.readyvr.util.DataCleanManager;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by MRKING on 2016/11/10.
 */

public class SettingsActivityPresenter implements SettingsActivityContract.Presenter {
    private SettingsActivityContract.View mView;
    private LoginRestClient mRestClient;
    private Context mContext;

    @Inject
    public SettingsActivityPresenter(SettingsActivityContract.View view, LoginRestClient restClient, Context context) {
        mView = view;
        mRestClient = restClient;
        mContext = context;
    }

    @Override
    public void getCacheLength() {
        Observable.create(new Observable.OnSubscribe<String>() {

            @Override
            public void call(Subscriber<? super String> subscriber) {
                if (!subscriber.isUnsubscribed()) {

                    try {
                        String length = DataCleanManager.getCacheSize(mContext.getCacheDir());
                        subscriber.onNext(length);
                        subscriber.onCompleted();
                    } catch (Exception e) {
                        subscriber.onError(e);
                    }

                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (mView != null)
                            mView.error(e);
                    }

                    @Override
                    public void onNext(String s) {
                        if (mView != null)
                            mView.getCacheLengthSuccess(s);
                    }
                });
    }

    @Override
    public void recycleToken(String token) {
        mRestClient.logout(token).map(new HttpResultFunc<LoginResponse>(RestApiUtils.LOGOUT_API))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LoginResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (mView != null)
                            mView.error(e);
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        if (mView != null)
                            mView.recycleTokenSuccess();
                    }
                });
    }

    @Override
    public void deleteCache() {
        Observable.create(new Observable.OnSubscribe<String>() {

            @Override
            public void call(Subscriber<? super String> subscriber) {
                if (!subscriber.isUnsubscribed()) {

                    try {
                        DataCleanManager.cleanInternalCache(mContext);
                        subscriber.onNext("");
                        subscriber.onCompleted();
                    } catch (Exception e) {
                        subscriber.onError(e);
                    }

                }
            }
        }).delay(1, TimeUnit.SECONDS).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (mView != null)
                            mView.error(e);
                    }

                    @Override
                    public void onNext(String s) {
                        if (mView != null)
                            mView.deleteCacheSuccess();
                    }
                });
    }

    @Override
    public void start() {

    }
}
